<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use App\Models\News;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;

class NewsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '消息';

    protected $status =[1=>'正常',2=>'禁用'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new News());
        
        $grid->column('title', __('Title'));
        $grid->column('images_url', __('Images url'))->image('', '50');
        $grid->column('content', __('Content'))->hide();
        $grid->column('author', __('Author'))->display(function ($id) {
            $admin = AdminUser::find($id);
            if (empty($admin)) {
                return '匿名';
            }
            return $admin['name'];
        });
        $grid->column('origin', __('Origin'));
        $grid->column('time', __('Time'))->display(function ($time){
        	$carbon = new Carbon($time);
        	return $carbon->diffForHumans();
            return date('Y-m-d H:i:s',$time);
        });
        $grid->column('status', __('Status'))->using($this->status);
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->like('title', __('Title'));
            $filter->equal('author', __('Author'))
                ->multipleSelect(AdminUser::where('store_id', Admin::user()->store_id)->pluck('name', 'id'));
            $filter->like('origin', __('Origin'));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(News::findOrFail($id));

        $show->field('title', __('Title'));
        $show->field('images_url', __('Images url'));
        $show->field('content', __('Content'))->textarea();
        $show->field('author', __('Author'));
        $show->field('origin', __('Origin'));
        $show->field('time', __('Time'))->as(function ($time){
            return date('Y-m-d H:i:s',$time);
        });
        $show->field('status', __('Status'))->using([
            1 => '正常',
            2 => '修整'
        ]);

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $admin = Auth::guard('admin')->user();
        $form = new Form(new News());

        $form->hidden('store_id', __('Store id'));
        $form->text('title', __('Title'))->rules("required|max:50|min:1");
        $form->image('images_url', __('Images url'))
            ->removable()
            ->uniqueName();
        
        $form->editor('content', __('Content'))->required();
        
        $form->select('author', '作者')->options(
            AdminUser::where('store_id', $admin->store_id)->get()->pluck('name', 'id')
        )->required();
        
        $form->text('origin', __('Origin'))->rules("required");;
        
        $form->time('time', __('Time'))
            ->format('YYYY-MM-DD HH:mm:ss')->rules("required");
        
        $form->hidden('sort', __('Sort'));
        
        $form->radio('status', __('Status'))
            ->options($this->status)->default(1);
        
        $form->saving(function (Form $form) {
            $form->store_id = Admin::user()->store_id;
            $form->sort = 1;
            $form->time = strtotime($form->time);
        });

        $form->footer(function ($footer) {
            $footer->disableViewCheck();
        });
        $form->tools(function ($tool) {
            $tool->disableView();
        });

        return $form;
    }
}
